<template>
    <el-card>
        <div slot="header">
            <span>修改密码</span>
        </div>
        <el-form ref="form" :model="form" label-width="100px" :rules="rules" status-icon>
            <el-form-item label="旧密码" prop="oldpwd">
                <el-input v-model="form.oldpwd" type="password" show-password></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newpwd">
                <el-input v-model="form.newpwd" type="password" show-password></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="newpwd2">
                <el-input v-model="form.newpwd2" type="password" show-password></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="change">修改</el-button>
                <el-button @click="reset">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>


<script>
import { checkOldpwd, editPwd } from '@/api/user.js'
export default {
    data () {
        //检验旧密码是否正确
        let checkpwd = async (rule, value, callback) => {
            if (value.length <= 12 && value.length >= 6) {
                await checkOldpwd({ oldPwd: value })
                callback();
            } else {
                callback(new Error('密码长度在 6 到 12 个字符'))
            }
        }
        //验证规则函数 
        let validatenewpwd2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.form.newpwd) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        }
        return {
            //定义的form数据
            form: {
                oldpwd: '',
                newpwd: '',
                newpwd2: ""
            },
            //验证规则
            rules: {
                oldpwd: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { validator: checkpwd, trigger: 'blur' }
                ],
                newpwd: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ],
                newpwd2: [
                    { validator: validatenewpwd2, trigger: 'blur' }
                ],


            }
        }
    },
    methods: {
        //点击修改
        change () {
            this.$refs.form.validate(async result => {
                if (result) {
                    let res = await editPwd({ newPwd: this.form.newpwd2 })
                    if (res.data.code === 0) {
                        this.$router.push('/login')
                    }
                } else {
                    console.log('修改失败');
                }
            })
        },
        //重置
        reset () {
            this.$refs.form.resetFields();
        }
    }
}
</script>

<style lang="less" scoped>
.el-from {
    width: 200px;
}

:deep(.el-input) {
    width: 200px
}
</style>